home *** CD-ROM | disk | FTP | other *** search
- From: jones@cais.cais.com (Ben Jones)
- Message-ID: <4h1pb6$n19@news2.cais.com>
- X-Original-Date: 28 Feb 1996 14:37:26 GMT
- Path: in2.uu.net!bounce-back
- Date: 28 Feb 96 14:57:55 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: Generic Object Callbacks
- Organization: Capital Area Internet Service info@cais.com 703-448-4470
- References: <pgpmoose.199602221531.14635@isolde.mti.sgi.com> <4gsi2p$905@bcarh8ab.bnr.ca> <4guh9e$jt6@sdaw04.seinf.abb.se>
- X-Newsreader: TIN [version 1.2 PL2]
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMTRtfeEDnX0m9pzZAQFXcwF9EZmIpTCvdAugsxZYh5H3iOB0Gs/FZ+uK
- dq+++QSKBFuVSMRotqEcOBnSvXrDyVaB
- =2xdO
-
- Anders Lindback (alindbac@sw.seisy.abb.se) wrote:
- : In article <4gsi2p$905@bcarh8ab.bnr.ca>,
- : brian (b.c.) white <bcwhite@bnr.ca> wrote:
- : >
- : >>What is needed is the ability to use member functions as callbacks
- : >>without any constraint on the type of the object they are invoked on.
- : >>This is possible in C++ only by using various implementation-dependent
- : >>hacks to escape the type system, because the language does not provide
- : >>any way to express such a construct.
- : >
- :
- : There exist a rather simple solution using a static member function.
- : Instead of having the callback call a normal member function it can
- : call a static callback member function with the object as a
- : argument. Then this callback function can call the real callback function.
- : This will involve an extra callback function for each callback one has,
- : but on the other hand it involves no changes to the base class if
- : one want to override the real callback function in a derived class.
-
- : class X
- : {
- : public:
- : static void callback(void* to)
- : virtual void real_callback();
- : };
-
- : void X::callback(void* to)
- : {
- : X* me = (X*)to;
- : me ->real_callback()
- : }
-
- : Motif widget callbacks and event handlers are examples where this pattern
- : works without problems.
-
- : If the desired class Y can not be modified it is possible that X is a helper
- : class that knows which member function on the real class Y it shall call.
- : I.e. this solution is portable and can be used for all possible classes.
-
- Granted, this works but it is still not typesafe since you are casting
- the object pointer to a void. It also forces you to clutter your class
- definitions with extra functions in order to allow for the possibility
- of callbacks (you can also define global functions which do the same
- thing but then you clutter the global namespace).
-
- The point we have been making is that there is a trivial way to extend
- the C++ language to provide for completely typesafe callbacks which
- are simple to use and which will dramatically improve the clarity
- of programs.
-
- Ben Jones
- Hughes Information Technology
- bjones@eos.hitc.com
- ---
- [ To submit articles: try just posting with your news-reader.
- If that fails, use mailto:std-c++@ncar.ucar.edu
- FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
- Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu.
- ]
-